//
//  RightModel.m
//  DeepBreathing
//
//  Created by rimi on 15/12/21.
//  Copyright © 2015年 rimi. All rights reserved.
//

#import "RightModel.h"


#import "NetworkManger+NetWorkKingManger.h"

@implementation RightModel
{
    NSInteger _page;
}

-(void)getDataWithPage:(NSInteger)page shouldAddPage:(BOOL)flag{
    [NetworkManger getFromCollectedWithPage:page successHander:^(id responseObject) {
        
        self.knowledge = responseObject;
        
        if (flag && [self.knowledge count]!= 0) {
            
            _page ++;
        }
               [NSKeyedArchiver archiveRootObject:self toFile:[RightModel archiveePath]];
    } failureHander:^(NSError *error) {
        
        self.knowledge = [NSNull null];
        
    }];
    
}

-(void)getData{
    [self getDataWithPage:1 shouldAddPage:NO];
}
-(void)getMoreData{
    [self getDataWithPage:_page shouldAddPage:YES];
}

+(NSString *)archiveePath{
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath =  paths.firstObject;
    
    return [basePath stringByAppendingString:@"rightModel.dat"];
    
}

-(instancetype)init{
    
    self  = [super init];
    
    id obj   =  [NSKeyedUnarchiver unarchiveObjectWithFile:[RightModel archiveePath]];
    
    if (obj) {
        self = obj;
    }
    return  self;
    
}

#pragma  mark -- coding
-(void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeObject:self.knowledge forKey:@"rightModel"];
}
-(id)initWithCoder:(NSCoder *)aDecoder{
    
    
    self = [super init];
    
    self.knowledge  = [aDecoder decodeObjectForKey:@"rightModel"];
    
    return self;
    
    
  }
@end
